package com.energy.analysis.report.service;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.energy.analysis.basic.entity.CollectData;
import com.energy.analysis.basic.mapper.CollectMapper;
import com.energy.analysis.comm.base.BaseMapper;
import com.energy.analysis.comm.base.BaseService;
import com.energy.analysis.comm.page.Page;
import com.energy.analysis.comm.utils.DateUtils;
import com.energy.analysis.report.entity.CollectDataDay;
import com.energy.analysis.report.mapper.CollectDataDayMapper;

@Service
public class CollectDataDayService extends BaseService<CollectDataDay> {

	@Autowired
	private CollectMapper collectMapper;
	
	@Autowired
	private CollectDataDayMapper dataCollectDayMapper;
	
	@Override
	protected BaseMapper<CollectDataDay> getMapper() {
		return dataCollectDayMapper;
	}
	 /**
	  * 每日处理机组信息方法,每日00:05处理上一日数据
	  * 数据处理时:(上一日-上二日 )  水、热、电数据
	  */
	public void dayDataCollectDataHandler(){
		//上一日日期
		List<CollectData> collectDataList1 = getDayData(-6);
		for (CollectData collectData : collectDataList1) {
			System.out.println(collectData);
		}
		//上二日日期
		List<CollectData> collectDataList2 = getDayData(-7);
		for (CollectData collectData : collectDataList2) {
			System.out.println(collectData);
		}
		//3.进行数据计算
		for (int i = 0; i < collectDataList1.size(); i++) {
			CollectData collectData1 = collectDataList1.get(i);
			Float ft3qTotal = collectData1.getFt3q();
			Float jqiTotal =  collectData1.getJqi();
			Float qqiTotal =  collectData1.getQqi();
			for (int j = 0; j < collectDataList2.size(); j++) {
				CollectData collectData2 = collectDataList2.get(j);
				if(collectData1.getStandId()!=null && collectData1.getStandId().equals(collectData2.getStandId())){
					ft3qTotal = ft3qTotal  - collectData2.getFt3q();
					jqiTotal =  jqiTotal - collectData2.getJqi();
					qqiTotal =  qqiTotal - collectData2.getQqi();
				}
			}
			CollectDataDay  collectDataDay = new CollectDataDay();
			collectDataDay.setId(collectData1.getId());
			collectDataDay.setStandId(collectData1.getStandId());
			collectDataDay.setStationId(collectData1.getStationId());
			collectDataDay.setGroupId(collectData1.getGroupId());
			collectDataDay.setCvi1(collectData1.getCvi1());
			collectDataDay.setFc1v1(collectData1.getFc1v1());
			collectDataDay.setFt1(collectData1.getFt1());
			collectDataDay.setFt1q(collectData1.getFt1q());
			collectDataDay.setFt2(collectData1.getFt2());
			collectDataDay.setFt2q(collectData1.getFt2q());
			collectDataDay.setFt3(collectData1.getFt3());
			collectDataDay.setFt3q(collectData1.getFt3q());
			collectDataDay.setJqi(collectData1.getJqi());
			collectDataDay.setLt1(collectData1.getLt1());
			collectDataDay.setOpTime(collectData1.getOpTime());
			collectDataDay.setTe1(collectData1.getTe1());
			collectDataDay.setTe2(collectData1.getTe2());
			collectDataDay.setTe3(collectData1.getTe3());
			collectDataDay.setTe4(collectData1.getTe4());
			collectDataDay.setPt1(collectData1.getPt1());
			collectDataDay.setPt2(collectData1.getPt2());
			collectDataDay.setPt3(collectData1.getPt3());
			collectDataDay.setPt4(collectData1.getPt4());
			collectDataDay.setQi(collectData1.getQi());
			collectDataDay.setQqi(collectData1.getQqi());
			collectDataDay.setFt3qPrice(collectData1.getFt3qPrice());
			collectDataDay.setQqiPrice(collectData1.getQqiPrice());
			collectDataDay.setJqiPrice(collectData1.getJqiPrice());
			collectDataDay.setQqiTotal(qqiTotal);
			collectDataDay.setJqiTotal(jqiTotal);
			collectDataDay.setFt3qTotal(ft3qTotal);
			collectDataDay.setDt(new java.sql.Date(collectData1.getOpTime().getTime()));
			//4.存入数据
			dataCollectDayMapper.insert(collectDataDay);
		}
	}
	private List<CollectData> getDayData(int num) {
		String dayBegin = DateUtils.dayAdd(num,"yyyy-MM-dd")+" 00:00:00";
		String dayEnd = DateUtils.dayAdd(num,"yyyy-MM-dd")+" 23:59:59";
		List<CollectData> collectDataList1 = collectMapper.getDayDataByOpTimeLast(dayBegin, dayEnd);
		return collectDataList1;
	}
	/**
	 * 日 月 年 季度报表
	 * @param page
	 * @return
	 */
	public List<Map<String,Object>> getDataByDateTime(Page<CollectDataDay> page){
		return dataCollectDayMapper.getDataByDateTime(page);
	}
	public int getDataByDateTimeCount(Page<CollectDataDay> page){
		try {
			return dataCollectDayMapper.getDataByDateTimeCount(page);
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}
	/**
	 * 经济分析报表
	 * @param page
	 * @return
	 */
	public List<Map<String,Object>> getDataEconByDateTime(Page<CollectDataDay> page){
		return dataCollectDayMapper.getDataEconByDateTime(page);
	}
	public int getDataEconByDateTimeCount(Page<CollectDataDay> page){
		return dataCollectDayMapper.getDataEconByDateTimeCount(page);
	}
	/**
	 * 获取最大日
	 */
	public java.sql.Date getMonthMaxDay(Integer year,Integer month,Integer quarter,String stationName){
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("year", year);
		param.put("month", month);
		param.put("quarter", quarter);
		param.put("stationName", stationName);
		return dataCollectDayMapper.getMonthMaxDay(param);
	}
	public java.sql.Date getMonthEconMaxDay(String beginTime, String endTime,String stationName) {
		return dataCollectDayMapper.getMonthEconMaxDay(beginTime,endTime,stationName);
	}
	
	/**
	 * 水耗分析数据获取
	 * @return
	 */
	public List<Map<String,Object>> getAnalysisWater(Page<CollectDataDay> page){
		DecimalFormat decimalFormat=new DecimalFormat("0.0");//构造方法的字符格式这里如果小数不足1位,会以0补足.
		//开始时间
		String beginTime =  (String) page.getParams().get("beginTime");
		Date bDateTime = DateUtils.parseDate(beginTime+" 00:00:00", "yyyy-MM-dd HH:mm:ss");
		
		page.getParams().put("dt", (String)page.getParams().get("endTime"));
		List<Map<String,Object>> listData = dataCollectDayMapper.getAnalysisWater(page);
		for (Map<String, Object> map : listData) {
			map.put("bDateTime", beginTime+" 00:00:00");
			map.put("eDateTime", DateUtils.toString((Date)map.get("op_time"), "yyyy-MM-dd HH:mm:ss"));
			
			
			float h =  (float) ((((Date)map.get("op_time")).getTime() - bDateTime.getTime())/(1000.0 * 60.0 * 60.0));
			map.put("hours",decimalFormat.format(h));
			float ft3q_total = (float)map.get("ft3q_total");
			float yxdh = (int)map.get("real_area")==0 ? ft3q_total : ft3q_total/(int)map.get("real_area");
			String yxdhstr=decimalFormat.format(yxdh);
			map.put("yxdhstr", yxdhstr);
			map.put("dt", map.get("dt").toString());
		}
		return listData;
	}
	public int getAnalysisWaterCount(Page<CollectDataDay> page) {
		return dataCollectDayMapper.getAnalysisWaterCount(page);
	}
	/**
	 * 电耗分析
	 * @param page
	 * @return
	 */
	public List<Map<String,Object>> getAnalysisElectricity(Page<CollectDataDay> page){
		DecimalFormat decimalFormat=new DecimalFormat("#.0");//构造方法的字符格式这里如果小数不足1位,会以0补足.
		//开始时间
		String beginTime =  (String) page.getParams().get("beginTime");
		Date bDateTime = DateUtils.parseDate(beginTime+" 00:00:00", "yyyy-MM-dd HH:mm:ss");
		
		page.getParams().put("dt", (String)page.getParams().get("endTime"));
		List<Map<String,Object>> listData = dataCollectDayMapper.getAnalysisElectricity(page);
		for (Map<String, Object> map : listData) {
			map.put("bDateTime", beginTime+" 00:00:00");
			map.put("eDateTime", DateUtils.toString((Date)map.get("op_time"), "yyyy-MM-dd HH:mm:ss"));
			float h =  (float) ((((Date)map.get("op_time")).getTime() - bDateTime.getTime())/(1000.0 * 60.0 * 60.0));
			map.put("hours",decimalFormat.format(h));
			BigDecimal jqi_total = new BigDecimal(String.valueOf(map.get("jqi_total")));
			BigDecimal real_area = new BigDecimal(String.valueOf(map.get("real_area")));
			String yxdhstr = "0";
			if(real_area.compareTo(BigDecimal.ZERO) == 1){
				BigDecimal yxdh =  jqi_total.divide( real_area);
				yxdhstr=decimalFormat.format(yxdh);
			}
			map.put("yxdhstr", yxdhstr);
			map.put("dt", beginTime);
		}
		return listData;
	}
	
	/**
	 * 热耗分析
	 * @param page
	 * @return
	 */
	public List<Map<String,Object>> getAnalysisHeat(Page<CollectDataDay> page){
		DecimalFormat decimalFormat=new DecimalFormat("#.0");//构造方法的字符格式这里如果小数不足1位,会以0补足.
		//开始时间
		String beginTime =  (String) page.getParams().get("beginTime");
		Date bDateTime = DateUtils.parseDate(beginTime+" 00:00:00", "yyyy-MM-dd HH:mm:ss");
		
		page.getParams().put("dt", (String)page.getParams().get("endTime"));
		List<Map<String,Object>> listData = dataCollectDayMapper.getAnalysisHeat(page);
		for (Map<String, Object> map : listData) {
			map.put("bDateTime", beginTime+" 00:00:00");
			map.put("eDateTime", DateUtils.toString((Date)map.get("op_time"), "yyyy-MM-dd HH:mm:ss"));
			float h =  (float) ((((Date)map.get("op_time")).getTime() - bDateTime.getTime())/(1000.0 * 60.0 * 60.0));
			map.put("hours",decimalFormat.format(h));
			BigDecimal qqi_total = new BigDecimal(String.valueOf(map.get("qqi_total")));
			BigDecimal real_area = new BigDecimal(String.valueOf(map.get("real_area")));
			String yxdhstr = "0";
			if(real_area.compareTo(BigDecimal.ZERO) == 1){
				BigDecimal yxdh =  qqi_total.divide( real_area);
				yxdhstr=decimalFormat.format(yxdh);
			}
			map.put("yxdhstr", yxdhstr);
			map.put("dt", beginTime);
		}
		return listData;
	}
	
	public int getAnalysisCount(Page<CollectDataDay> page) {
		return dataCollectDayMapper.getAnalysisCount(page);
	}
}
